﻿using System.Configuration;
using System.Data;
using System.Windows;
using System.Collections.Specialized;
using System.Windows.Input;
using Clinic.Views.Main;
using Clinic.View;
using HandyControl.Controls;
using Clinic.IService;
using Clinic.Service;
using Clinic.View.ViewModels;
using Clinic.ViewModels.Main;
using Clinic.View.Views;
using CommunityToolkit.Mvvm.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using Clinic.Extensions;
using System.Reflection;
using System.IO;

namespace Clinic
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        /// <summary>
        /// 应用程序配置管理器
        /// </summary>
        public static System.Configuration.Configuration Configuration { get; set; }

        public App()
        {
            Exit += App_Exit;
            Configuration = GetConfiguration();
        }

        private void App_Exit(object sender, ExitEventArgs e)
        {
            Environment.Exit(0);
        }

        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            Ioc.Default.ConfigureServices(
                new ServiceCollection()
                .AddSingleton<ILoginService, LoginService>()
                .AddSingleton<IUserService, UserService>()
                .AddSingleton<IRoleService, RoleService>()
                .AddTransient(typeof(PageViewModel<>))
                .AddTransient<RoleViewModel>()
                .AddTransient<HomeViewModel>()
                .AddTransient<MenuViewModel>()
                .AddTransient<MainContentViewModel>()
                .RegisterView<RoleView>()
                .RegisterView<UserView>()
                .RegisterView<MenuManagerView>()
                .BuildServiceProvider());

            //.AddSingleton(RestService.For<IRedditService>("https://www.reddit.com/"))
            //.AddSingleton(RestService.For<IContactsService>("https://randomuser.me/"))
            //[Get("/api/?dataType=json&inc=name,email,picture")]
            //Task<ContactsQueryResponse> GetContactsAsync([AliasAs("results")] int count);
        }

        /// <summary>
        /// 获取应用程序配置文件App.config
        /// </summary>
        /// <returns></returns>
        private System.Configuration.Configuration GetConfiguration()
        {
            ExeConfigurationFileMap map = new ExeConfigurationFileMap();
            Assembly assembly = Assembly.GetCallingAssembly();
            Uri uri = new Uri(Path.GetDirectoryName(assembly.CodeBase));
            map.ExeConfigFilename = Path.Combine(uri.LocalPath, "App.config");
            System.Configuration.Configuration configuration = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(map, 0);
            return configuration;
        }
    }
}
